//
//  LineWidthToolsView.swift
//  SmartPenSwift
//
//  Created by tql on 2021/2/3.
//

import UIKit

class LineWidthToolsView: UIView {

    var _penColor : UIColor = UIColor.black
    
    var currentWidth : Int = 1
    var penColor : UIColor{
        get{
//            print( penColor);
            return _penColor
        }set{
            
            _penColor = newValue
            for  sbbtn : UIView in self.subviews {
                sbbtn.removeFromSuperview()
            }
            self .initUI()
        }
        
        
    }
//    override var backgroundColor: UIColor?{
//        get{
//            
//        }
//        set{
//            
//        }
//    }
    
    typealias widthChangeBlock = (_ changeWidth : Int) ->()
    var outBlock : widthChangeBlock?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white;
        initUI()
    }
   
     
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func initUI() -> Void {
        let btnwidth : CGFloat  = 50
        let gap : CGFloat  = 20
        let btnHeight : CGFloat = 50
        var marginGap : CGFloat = (   (kScreenWidth) - btnwidth * 5 - gap * 4) / 2.0
        
        var imageArray : [String] = ["Path_LineWidth_1","Path_LineWidth_2","Path_LineWidth_3","Path_LineWidth_4","Path_LineWidth_5"]
        
        let imageSelectedArray : [String] = ["选中-1px","选中-2px","选中-3px","选中-4px","选中-5px"]
        
        if _penColor .isEqual(UIColor.customBlack()) {
            imageArray =  ["Path_LineWidth_1","Path_LineWidth_2","Path_LineWidth_3","Path_LineWidth_4","Path_LineWidth_5"]
        }else if _penColor .isEqual(UIColor.customRed()) {
            imageArray =  ["Path_LineWidth_red_1","Path_LineWidth_red_2","Path_LineWidth_red_3","Path_LineWidth_red_4","Path_LineWidth_red_5"]
        }else if _penColor .isEqual(UIColor.customBlue()) {
            imageArray =  ["Path_LineWidth_blue_1","Path_LineWidth_blue_2","Path_LineWidth_blue_3","Path_LineWidth_blue_4","Path_LineWidth_blue_5"]
        }else if _penColor .isEqual(UIColor.customGreen()) {
            imageArray =  ["Path_LineWidth_green_1","Path_LineWidth_green_2","Path_LineWidth_green_3","Path_LineWidth_green_4","Path_LineWidth_green_5"]
        }else if _penColor .isEqual(UIColor.customYellow()) {
            imageArray =  ["Path_LineWidth_yellow_1","Path_LineWidth_yellow_2","Path_LineWidth_yellow_3","Path_LineWidth_yellow_4","Path_LineWidth_yellow_5"]
        }
        else if _penColor .isEqual(UIColor.customOrange()) {
            imageArray =  ["Path_LineWidth_orange_1","Path_LineWidth_orange_2","Path_LineWidth_orange_3","Path_LineWidth_orange_4","Path_LineWidth_orange_5"]
        }
        
        for  i : Int in 0..<imageArray.count {
            let  btn : UIButton = makeButton(imageArray[i], imageSelectedArray[i], nil)
            btn.frame = CGRect(x: marginGap, y: 10, width: btnwidth, height: btnHeight)
            marginGap += gap + btnwidth
            btn.tag =  i + 1
            self.addSubview(btn)
        }
        
    }
    
    
    
    

}

extension LineWidthToolsView{
    @objc func btnclick(_ btn : UIButton) -> Void {
        unselectAllBtn(btn)
        if btn.isSelected == true {
            return
        }
        btn.isSelected = !btn.isSelected
        
        if btn.isSelected == true {
            if outBlock != nil {
                outBlock!(btn.tag)
            }
        }
    }
    @objc func unselectAllBtn(_ btn : UIButton ){
        let btns  = self.subviews
        for  btnin :  UIView  in btns {
            if btnin .isKind(of: UIButton.classForCoder()) {
                if btn .isEqual(btnin) {
                    continue
                }
                else{
                    btn.isSelected = false
                }
            }else{
                continue
            }
            
        }
        
    }
    
    func makeButton(_ normalImage : String?, _ selectImage : String?, _ title : String?) -> UIButton {
        let btn : UIButton = UIButton.init()
        if normalImage != nil  {
            btn.setImage(UIImage.init(named: normalImage!), for: .normal)
        }
        if selectImage != nil  {
            btn.setImage(UIImage.init(named: selectImage!), for: .selected)
        }
        if title != nil  {
            btn.setTitle(title, for: .normal)
        }
        btn.addTarget(self, action: #selector(btnclick(_:) ), for:.touchUpInside )
        
        return btn
        
    }
}
